package com.wander.patterns.act.iterator;

import lombok.Data;

import java.util.ArrayList;
import java.util.List;

/**
 * @description:自定义的聚合类  对外提供的api
 * @author: zhuyong
 * @create: 2019-09-07 17:07
 **/
@Data
public class ConcreteMyAggregate {
    private List<Object> list = new ArrayList<>();

    public ConcreteMyAggregate(List<Object> list) {
        this.list = list;
    }

    public void addObj(Object o){
        this.list.add(o);
    }
    public void removeObj(Object o){
        this.list.remove(o);
    }

    public ConcreteIterator getConcreteIterator(){
        return new ConcreteIterator();
    }

class ConcreteIterator implements MyIterator{
    /**
     * 游标定义当前遍历的下标
      */
    private int cursor=0;

    public int getCursor() {
        return cursor;
    }

    public void setCursor(int cursor) {
        this.cursor = cursor;
    }

    @Override
    public Object next() {
        Object o = cursor>=list.size()?null:list.get(cursor);
        cursor++;
        return o;
    }

    @Override
    public Object first() {
        return list.get(0);
    }

    @Override
    public boolean isFirst() {
        return cursor == 0 ? true : false;
    }

    @Override
    public boolean hasNext() {
        return cursor < list.size()? true :false;
    }

}


}
